<template>
  <div class="classes-wrapper">
    <div class="classes-header-wrapper">
      <div class="classes-header-search-wrapper">
        <el-input class="classes-header-search-input"
                  prefix-icon="el-icon-search"
                  v-model="classesName"
                  placeholder="请输入班级进行搜索"></el-input>
        <el-button class="classes-header-search-button"
                   icon="el-icon-search"
                   @click="getClasses"
                   type="primary">搜索</el-button>
      </div>
      <div class="classes-header-operation-wrapper">
        <el-button class="classes-header-add-button"
                   icon="el-icon-plus"
                   @click="showAddClassesDialog"
                   type="primary">添加班级</el-button>
        <el-button class="classes-header-delete-batch-button"
                   icon="el-icon-delete"
                   @click="deleteClassesByBatch"
                   :disabled="this.multipleSelections.length === 0"
                   type="danger">批量删除</el-button>
      </div>
    </div>
    <div class="classes-table-wrapper">
      <el-table
              class="classes-table"
              v-loading="loading"
              element-loading-text="拼命加载中"
              @selection-change="handleSelectionChange"
              :data="classes"
              stripe>
        <el-table-column
                type="selection"
                width="70">
        </el-table-column>
        <el-table-column
                prop="id"
                label="编号"
                width="110">
        </el-table-column>
        <el-table-column
                prop="name"
                label="班级"
                width="250">
        </el-table-column>
        <el-table-column
                prop="major.name"
                label="所属专业"
                width="250">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini"
                       @click="showEditClassesDialog(scope.row)"
                       type="primary">编辑</el-button>
            <el-button type="danger"
                       @click="deleteClasses(scope.row)"
                       size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="classes-table-pagination-wrapper">
        <el-pagination
                background
                @current-change="currentChange"
                @size-change="sizeChange"
                layout="sizes, prev, pager, next, jumper, ->, total, slot"
                :total="total">
        </el-pagination>
      </div>
    </div>
    <el-dialog class="dialog"
               width="25%"
               :title="title"
               :visible.sync="dialogFormVisible">
      <div class="dialog-form-wrapper">
        <el-form class="dialog-form"
                 :rules="rules"
                 :model="clazz"
                 ref="classesForm">
          <el-row>
            <el-col :span="24">
              <el-form-item label="班级:" prop="name">
                <el-input v-model="clazz.name"
                          prefix-icon="el-icon-edit"
                          placeholder="请输入班级"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="所属专业:" prop="majorId">
                <el-select v-model="clazz.majorId"
                           placeholder="请选择专业">
                  <el-option
                          v-for="item in majors"
                          :key="item.id"
                          :label="item.name"
                          :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogClasses">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "EduClasses",
    data() {
      return {
        classes: [],
        classesName: '',
        majors: '',
        multipleSelections: '',
        loading: false,
        currentPage: 1,
        title: '',
        total: 0,
        size: 10,
        dialogFormVisible: false,
        clazz: {
          id: null,
          name: '',
          majorId: null,
        },
        rules: {
          name: [{ required: true, message: '请输入班级', trigger: 'blur' }],
          majorId: [{ required: true, message: '请选择所属专业', trigger: 'blur' }]
        }
      }
    },
    mounted() {
      this.getClasses()
      this.getMajors()
    },
    methods: {
      //  删除班级
      deleteClasses(data) {
        this.$confirm('此操作将永久删除[' + data.name + ']班级的信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/edumanagement/classes/' + data.id).then(resp => {
            if (resp) {
              this.getClasses()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  获取班级
      getClasses() {
        this.loading = true
        this.getRequest('/edumanagement/classes/?currentPage=' + this.currentPage
            + '&size=' + this.size).then(resp => {
          if (resp) {
            this.classes = resp.data
            this.total = resp.total
          }
          this.loading = false
        })
      },
      //  获取所有专业
      getMajors() {
        this.getRequest('/edumanagement/classes/major').then(resp => {
          if (resp) {
            this.majors = resp
          }
        })
      },
      //  当分页组件的每页显示多少条数据改变时
      sizeChange(size) {
        this.size = size
        this.currentPage = 1
        this.getClasses()
      },
      //  当分页组件的页数改变时
      currentChange(currentPage) {
        this.currentPage = currentPage
        this.getClasses()
      },
      //  展示添加班级dialog
      showAddClassesDialog() {
        this.title = '添加班级'
        this.clazz = {
          id: null,
          name: '',
          majorId: null,
        }
        this.dialogFormVisible = true
      },
      //  展示更新班级dialog
      showEditClassesDialog(data) {
        this.title = '更新班级'
        //  this.updatePos = data 这个直接赋值会导致bug，输入框与table里的数据同步变化
        //  Object.assign(target, source1, source2)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target)
        Object.assign(this.clazz, data)
        this.dialogFormVisible = true
      },
      deleteClassesByBatch() {
        this.$confirm('此操作将永久删除这['+ this.multipleSelections.length +']个班级, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = '?'
          this.multipleSelections.forEach(item => {
            ids += 'ids=' + item.id + '&'
          })
          this.deleteRequest('/edumanagement/classes/' + ids).then(resp => {
            if (resp) {
              this.getClasses()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  获取被选中的班级
      handleSelectionChange(data) {
        this.multipleSelections = data
      },
      dialogClasses() {
        if (this.clazz.id) {
          // 更新班级
          this.$refs['classesForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.putRequest('/edumanagement/classes/', this.clazz).then(resp => {
                if (resp) {
                  this.getClasses()
                }
              })
            }
          })
        } else {
          //  添加学生
          this.$refs['classesForm'].validate((valid) => {
            if (valid) {
              this.dialogFormVisible = false
              this.postRequest('/edumanagement/classes/', this.clazz).then(resp => {
                if (resp) {
                  this.getClasses()
                }
              })
            }
          })
        }
      }
    }
  }
</script>

<style lang="less">
  
  .classes-wrapper{
    width: 90%;
    margin-top: 20px;
    margin-left: 40px;

    .classes-header-wrapper {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;
      box-sizing: border-box;
      padding: 20px 20px;
      /*background-color: #EBEEF5;*/
      height: 80px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;

      .classes-header-search-wrapper {


        .classes-header-search-input {
          width: 220px;
          margin-right: 10px;
        }

        .classes-header-search-button {
          height: min-content;
        }
      }

      .classes-header-operation-wrapper {

        .classes-header-add-button {

        }

        .classes-header-delete-batch-button {

        }
      }
    }

    .classes-table-wrapper {
      margin-top: 20px;
      box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
      border-radius: 4px;
      padding: 5px 20px 20px 20px;

      .classes-table {
        width:100%;
      }

      .classes-table-pagination-wrapper {
        margin-top: 20px;
        display: flex;
        justify-content: center;
      }
    }
  }
</style>